package com.heima.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.media.dto.SensitiveDto;
import com.heima.media.entity.WmSensitive;
import com.heima.media.mapper.WmSensitiveMapper;
import com.heima.media.service.IWmSensitiveService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 敏感词信息表 服务实现类
 * </p>
 *
 * @author itheima
 * @since 2021-12-25
 */
@Service
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements IWmSensitiveService {


    @Override
    public ResponseResult saveSensitive(WmSensitive entity) {
        if (entity == null || StringUtils.isEmpty(entity.getSensitives())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 已存在的敏感词则不能保存
        LambdaQueryWrapper<WmSensitive> query = new LambdaQueryWrapper<>();
        query.eq(WmSensitive::getSensitives, entity.getSensitives());
        int count = this.count(query);
        if (count > 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        // 保存敏感词
        this.save(entity);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult listByName(SensitiveDto dto) {
        // 根据敏感词模糊分页查询
        IPage<WmSensitive> page = new Page<>(dto.getPage(), dto.getSize());
        // 构建查询请求
        LambdaQueryWrapper<WmSensitive> query = new LambdaQueryWrapper<>();
        // 判断传入的敏感词是否为空
        if (!StringUtils.isEmpty(dto.getName())) {
            query.like(WmSensitive::getSensitives, dto.getName());
        }
        // 按照时间倒序排
        query.orderByDesc(WmSensitive::getCreatedTime);
        IPage<WmSensitive> iPage = this.page(page, query);
        // 构建通用的分页响应
        PageResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(),
                iPage.getTotal(), iPage.getRecords());
        return result;
    }

    @Override
    public ResponseResult updateSensitive(WmSensitive adSensitive) {
        // 根据id进行更新
        if (adSensitive == null || StringUtils.isEmpty(adSensitive.getSensitives()) || adSensitive.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 判断更新后的名称是否已经存在
        LambdaQueryWrapper<WmSensitive> query = new LambdaQueryWrapper<>();
        query.eq(WmSensitive::getSensitives, adSensitive.getSensitives());
        query.ne(WmSensitive::getId, adSensitive.getId());
        int count = this.count(query);
        if (count > 0) {
            // 前端返回异常提示
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        // 更新
        this.updateById(adSensitive);
        return ResponseResult.okResult();
    }

}
